<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--分类数据-->
      <el-col :span="5" :xs="24">
          <div class="head-container" style="margin-bottom: 10px" v-if="selectCategory">
              <el-alert :title="selectCategory" type="info" close-text="取消选中" show-icon @close="cancelSelect" />
          </div>
          <div class="head-container">
              <el-input
                      v-model="categoryName"
                      placeholder="请输入分类名称"
                      clearable
                      prefix-icon="Search"
                      style="margin-bottom: 20px"
              />
          </div>
          <div class="head-container">
              <el-tree
                      :data="categoryOptions"
                      :props="{ label: 'label', children: 'children' }"
                      :expand-on-click-node="false"
                      :filter-node-method="filterNode"
                      ref="categoryTreeRef"
                      node-key="id"
                      highlight-current
                      default-expand-all
                      @node-click="handleNodeClick"
              />
          </div>
      </el-col>
      <!--流程数据-->
      <el-col :span="19" :xs="24">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="名称" prop="name">
                  <el-input
                          v-model="queryParams.name"
                          placeholder="请输入名称"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
          </el-form>
          <el-table v-loading="loading" fit :data="definitionList" border >
              <el-table-column label="流程编号" align="center" prop="deploymentId" :show-overflow-tooltip="true"/>
              <el-table-column label="流程标识" align="center" prop="flowKey" :show-overflow-tooltip="true"/>
              <el-table-column label="流程名称" align="center" width="140" :show-overflow-tooltip="true">
                  <template #default="scope">
                      <el-button link type="primary" @click="handleReadImage(scope.row.deploymentId)">
                          <span>{{ scope.row.name }}</span>
                      </el-button>
                  </template>
              </el-table-column>
              <el-table-column label="流程版本" align="center" width="80">
                  <template #default="scope">
                      <el-tag effect="plain">v{{ scope.row.version }}</el-tag>
                  </template>
              </el-table-column>
              <el-table-column label="部署时间" align="center" prop="deploymentTime"/>
              <!--              <el-table-column label="状态" align="center">-->
<!--                  <template #default="scope">-->
<!--                      <el-tag effect="dark" v-if="scope.row.suspensionState === 1">激活</el-tag>-->
<!--                      <el-tag type="danger" effect="dark" v-if="scope.row.suspensionState === 2">挂起</el-tag>-->
<!--                  </template>-->
<!--              </el-table-column>-->
              <el-table-column label="操作" width="150" fixed="right" class-name="small-padding fixed-width">
                  <template #default="scope">
                     <el-button size="small" icon="Position" link type="primary" @click="handleStartProcess(scope.row)">发起任务</el-button>
                  </template>
              </el-table-column>
          </el-table>

          <pagination
                  v-show="total>0"
                  :total="total"
                  v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize"
                  @pagination="getList"
          />
      </el-col>
    </el-row>

    <!-- 流程图 -->
    <el-dialog :title="readImage.title" v-model="readImage.open" width="70%" append-to-body>
      <bpmn-viewer :flowData="flowData"/>
    </el-dialog>
  </div>
</template>

<script setup name="Apply">
import {
  applyList,
  flowXmlAndNode
} from "@/api/flowable/definition";
import { categoryTree } from "@/api/flowable/category";
import BpmnViewer from '@/components/Process/viewer';
import router from "@/router";

const { proxy } = getCurrentInstance();

const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 流程定义表格数据
const definitionList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);
const deployId = ref('');
// xml
const flowData = ref({});
const vFormRef = ref(null)
// 流程分类
const selectCategory = ref(null)
const categoryName = ref(null)
const categoryOptions = ref(undefined);

const data = reactive({
  formData: {}, // 默认表单数据
  readImage:{
    open: false,
    title: "",
  },
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null
  },
  formQueryParams:{
    pageNum: 1,
    pageSize: 10,
  },
  // 挂载表单到流程实例
  formDeployParam:{
    formId: null,
    deployId: null
  },
});

const { queryParams, formData, readImage } = toRefs(data);

/** 根据分类名称筛选树 */
watch(categoryName, val => {
    proxy.$refs["categoryTreeRef"].filter(val);
});

/** 查询流程定义列表 */
function getList() {
    loading.value = true;
    applyList(queryParams.value).then(response => {
        definitionList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
    });
}

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
    if (!value) return true;
    return data.label.indexOf(value) !== -1;
};

/** 节点单击事件 */
function handleNodeClick(data) {
    if (data.parentId === 0) {
        proxy.$modal.msgWarning("父级节点只做分类展示,请选择子节点进行查询!");
    } else {
        queryParams.value.category = data.id;
        selectCategory.value = data.label;
        handleQuery();
    }
}

/** 取消选中 */
function cancelSelect() {
    selectCategory.value = null;
    queryParams.value.category = null;
    proxy.$refs.categoryTreeRef.setCurrentKey(null);
    handleQuery();
}

/** 查询分类下拉树结构 */
function getCategoryTree() {
    categoryTree().then(response => {
        categoryOptions.value = response.data;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.category = null;
  proxy.$refs.categoryTreeRef.setCurrentKey(null);
  handleQuery();
}


/**  发起流程申请 */
function handleStartProcess(row){
    router.push({ path: '/flowable/task/apply/send/index',
        query: {
            deployId: row.deploymentId,
            procDefId: row.id
        }
    })
}

/** 流程图查看 */
function handleReadImage(deployId){
  readImage.value.title = "流程图";
  readImage.value.open = true;
  flowXmlAndNode({deployId:deployId}).then(res => {
    flowData.value = res.data;
  })
}

getCategoryTree();
getList();
</script>
